package com.reone.jsbridgedemo.activity;

import android.os.Bundle;
import android.view.View;

import com.reone.jsbridgedemo.R;
import com.reone.jsbridgedemo.activity.base.BaseActivity;
import com.reone.talklibrary.TalkApp;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import butterknife.ButterKnife;

public class X5WebViewActivity extends BaseActivity {

    String testUrl = "https://x5.tencent.com/tbs/guide/sdkInit.html";

    @BindView(R.id.x5_web_view)
    WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_x5_webview);
        ButterKnife.bind(this);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setDisplayZoomControls(false);
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.getSettings().setAllowFileAccess(true);
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
                super.onShowCustomView(view, customViewCallback);
                TalkApp.talk("onShowCustomView");
            }

            @Override
            public void onShowCustomView(View view, int i, IX5WebChromeClient.CustomViewCallback customViewCallback) {
                super.onShowCustomView(view, i, customViewCallback);
                TalkApp.talk("onShowCustomView i " + i);
            }

            @Override
            public void onHideCustomView() {
                super.onHideCustomView();
                TalkApp.talk("onHideCustomView");
            }
        });
        webview.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return false;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        TalkApp.talk("load start url " + testUrl);
        webview.loadUrl(testUrl);
    }
}
